package com.tianzhu.web;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping
@Slf4j
public class SecurityController {

    public static final int MINUTES = 60;
    
    /*@RequestMapping("/login")
    public String login() {
        return "pages/login";
    }*/
    
    
    @RequestMapping("/login")
    public String login(
            @RequestParam(value="error", required = false)
            String error,
            @RequestParam(value="logout", required = false)
            String logout,
            Model model){

        if(error != null){
            model.addAttribute("error", "Invalid username and password");
        }

        if (logout !=null){
            model.addAttribute("msg", "You have been logged out successfully");
        }

        return "pages/login";
    }

    @RequestMapping(value = "/login/success")
    public String loginSuccess() {
        return "redirect:/home";
    }

    /*@RequestMapping("/logout")
    public String logoutSwitchedUser() {
        return "redirect:/normallogout";
    }*/
    
    @RequestMapping(value = "/login/failure")
    public String loginFailure(Model model) {
        String message = "Wrong password or username provided! Please try again.";
        model.addAttribute("message", message);
        return "pages/login";
    }

    @RequestMapping(value = "/logout/success")
    public String logoutSuccess() {
        return "redirect:/home";
    }
    
    @RequestMapping("/switchuser")
    public String switchUser() {
        return "pages/switchuser";
    }
    
    @RequestMapping("/switchuserto{username}")
    public String switchUserUsername(@PathVariable String username) {
        return "redirect:/switchuserto?username=" + username;
    }
    
    @RequestMapping(value = "/accessdenied")
    public String accessDenied() {
        return "pages/accessdenied";
    }
}